﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using Benefit.Models.Sys;
namespace Benefit.Service.Day
{
    public class ServiceCompanyAvgDay : Benefit.Interface.Day.ICompanyAvgDay
    {
        Benefit.DB.DBManager db = null;
        public ServiceCompanyAvgDay(Benefit.DB.DBManager db)
        {
            this.db = db;
        }

        /// <summary>
        /// 删除当日所有记录
        /// </summary>
        /// <param name="weekId"></param>
        public void Delete(int tradeHistoryId)
        {
            var query = from t in db.CompanyAvgDay where t.TradeHistoryId == tradeHistoryId select t;
            foreach (Models.Day.CompanyAvgDay c in query)
            {
                db.CompanyAvgDay.Remove(c);
            }
            db.SaveChanges();
        }

        /// <summary>
        /// 保存日均值
        /// </summary>
        /// <param name="tradeHistoryId">插入时间编号</param>
        /// <param name="pdate">插入时间</param>
        public void InitCompanyDayAvg(int tradingHistoryId)
        {
            Interface.ServerData.IT_Account ita = new ServerData.ServiceT_Account(db);
            List<Models.ServerData.T_Account> serverAccounts = ita.GetList();
            Models.Day.CompanyAvgDay change = new Models.Day.CompanyAvgDay();
            var query = (from t in db.CompanyAvgDay where t.TradeHistoryId == tradingHistoryId select t).ToList();
            var query2 = (from t in db.AccountDayChange where t.TradeHistoryId == tradingHistoryId select t).ToList();
            var query3 = (from t in db.AccountDayAnalysis where t.TradeHistoryId == tradingHistoryId select t).ToList();
            Interface.Sys.IOperator iop = new Sys.ServiceOperator(db);
            var operators = iop.GetUnDeletedOperators();

            if (query.Count() > 0)//有本月数据
            {
                change = query.First();
            }
            if (query2.Count() > 0)
            {
                change.BillCount = Math.Round(query2.Sum(a => a.BillCount) / operators.Count(), 2);
                change.DayCount = Math.Round(query2.Sum(a => a.DayCount) / operators.Count(), 2);
                change.Free = Math.Round(query2.Sum(a => a.Free) / operators.Count(), 2);
                change.LoseCount = Convert.ToInt32(query2.Sum(a => a.LoseCount) / operators.Count());
                change.Profit = Math.Round(query2.Sum(a => a.Profit) / operators.Count(), 2);
                change.SumLost = Math.Round(query2.Sum(a => a.SumLost) / operators.Count(), 2);
                change.SumWin = Math.Round(query2.Sum(a => a.SumWin) / operators.Count(), 2);
                change.UsedMargin = Math.Round(query2.Sum(a => a.UsedMargin) / operators.Count(), 2);
                change.WinCount = Convert.ToInt32(query2.Sum(a => a.WinCount) / operators.Count());
            }
            if (query3.Count() > 0)
            {
                change.AvgBillBenefit = Math.Round(query3.Sum(a => a.AvgBillBenefit) / operators.Count(), 2);
                change.AvgPostionTime = Math.Round(query3.Sum(a => a.AvgPostionTime) / operators.Count(), 2);
                change.MaxReturn = Math.Round(query3.Sum(a => a.MaxReturn) / operators.Count(), 2);
                change.OverStopLoss = Math.Round(query3.Sum(a => a.OverStopLoss) / operators.Count(), 2);
                change.PingJinPing = Convert.ToInt32(query3.Sum(a => a.PingJinPing) / operators.Count());
                change.RaiseRate = Math.Round(query3.Sum(a => a.RaiseRate) / operators.Count(), 2);
                change.RiskCount = Convert.ToInt32(query3.Sum(a => a.RiskCount) / operators.Count());
                change.Utilization = Math.Round(query3.Sum(a => a.Utilization) / operators.Count(), 2);
                change.WinRate = Math.Round(query3.Sum(a => a.WinRate) / operators.Count(), 2);
                change.Yield = Math.Round(query3.Sum(a => a.Yield) / operators.Count(), 2);
            }
            change.TradeHistoryId = tradingHistoryId;
            if (query.Count() == 0)//没有本月数据
            {
                db.CompanyAvgDay.Add(change);
            }
            db.SaveChanges();
        }



        /// <summary>
        /// 获取公司日均值
        /// </summary>
        /// <param name="accountid"></param>
        /// <param name="tradeHistoryId"></param>
        /// <returns></returns>
        public Models.Day.CompanyAvgDay GetCompanyAvgDay(int tradeHistoryId)
        {
            Models.Day.CompanyAvgDay count = new Models.Day.CompanyAvgDay();
            var query = db.CompanyAvgDay.Where(a => a.TradeHistoryId == tradeHistoryId);
            if (query.Count() > 0)
            {
                count = query.First();
            }
            return count;
        }
    }
}